package demo1;

import java.io.*;
import java.net.*;

public class MyClient {
    public static void main(String[] args) {
        try {
            Socket socket = new Socket("127.0.0.1",1680);
            PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));

            System.out.print("已连接到服务器，请输入消息：");
            String s;
            while ((s = sin.readLine()) != null) {
                out.println(s);

                // 检查是否结束通信
                if ("bye".equalsIgnoreCase(s)) {
                    System.out.println("连接正在关闭... ...");
                    break;
                }

                // 等待服务器响应
                String serverResponse = in.readLine();
                if (serverResponse != null) {
                    System.out.println("@ 服务器回复: " + serverResponse);

                    // 检查服务器是否要结束通信
                    if ("bye".equalsIgnoreCase(serverResponse)) {
                        System.out.println("服务器已关闭连接");
                        break;
                    }else {
                        System.out.print("请输入消息");
                    }
                }
            }
            out.close();
            in.close();
            socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


//package demo1;
//
//import java.io.*;
//import java.net.*;
//
//public class MyClient {
//    public static void main(String[] args) {
//        try (Socket socket = new Socket("127.0.0.1", 1680);
//             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
//             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//             BufferedReader sin = new BufferedReader(new InputStreamReader(System.in))) {
//
//            System.out.println("已连接到服务器，请输入消息:");
//
//            // 创建单独的线程来接收服务器消息
//            Thread receiveThread = new Thread(() -> {
//                try {
//                    String serverResponse;
//                    while ((serverResponse = in.readLine()) != null) {
//                        if ("bye".equalsIgnoreCase(serverResponse)) {
//                            System.out.println("服务器已关闭连接");
//                            System.exit(0);
//                        }
//                        System.out.println("@ 服务器回复: " + serverResponse);
//                        System.out.print("请输入消息：");
//                    }
//                } catch (IOException e) {
//                    System.out.println("与服务器的连接已断开");
//                }
//            });
//            receiveThread.start();
//
//            // 主线程发送消息
//            String userInput;
//            while ((userInput = sin.readLine()) != null) {
//                if ("bye".equalsIgnoreCase(userInput)) {
//                    out.println("bye");
//                    System.out.println("连接正在关闭... ...");
//                    break;
//                }
//                out.println(userInput);
//            }
//        } catch (IOException e) {
//            System.err.println("连接服务器失败: " + e.getMessage());
//        }
//    }
//}